﻿using System;
using System.Collections.Generic;
using Evpo.ContactData.Common;
using Evpo.ContactData.Data;
using System.Data;
using System.Linq;

namespace Evpo.ContactData.ViewModel
{
    public class NameLookupViewModel
    {
        public NameLookupViewModel(IConnection conn, string lookupName)
        {
            _conn = (Connection)conn;
            _lookupName = lookupName;
            CreateAdapter();
        }

        #region Fields
        private readonly string _lookupName;
        private readonly Connection _conn;
        private IEditableDictionaryItemAdapter _adapter;
        #endregion

        #region Properties
        #endregion

        #region Methods

        public string GetLookupDisplayName()
        {
            return _adapter.GetDisplayName();
        }

        private void CreateAdapter()
        {
            switch (_lookupName)
            {
                case "HomeTown":
                    _adapter = new HomeTownAdapter(_conn);
                    break;
                case "ContactInterest":
                    _adapter = new ContactInterestAdapter(_conn);
                    break;
                case "Title":
                    _adapter = new TitleAdapter(_conn);
                    break;
                default:
                    throw new ApplicationException("LookupName " + _lookupName + " is not known");
            }
        }

        private DictionaryItem CreateNewItem()
        {
            return _adapter.CreateNewItem();    
        }

        public int GetIDForUnspecified()
        {
            return _adapter.GetIDForUnspecified();
        }

        #endregion Methods

        #region Data Source
        public static List<DictionaryItem> Select(int pageIndex, int pageCount, NameLookupViewModel model)
        {
            int unspecified = model._adapter.GetIDForUnspecified();
            int orderNum = -1;
            return (from item in model._adapter.GetItems()
                    where (++orderNum) >= pageIndex && orderNum < (pageIndex + pageCount) && item.ID != unspecified
                    select item).ToList();
        }

        public static int SelectCount(int pageIndex, int pageCount, NameLookupViewModel model)
        {
            return model._adapter.GetItems().Count;
        }

        public static void Insert(string name, NameLookupViewModel model)
        {
            DictionaryItem item = model.CreateNewItem();
            item.Name = name;
            model._adapter.Save(item, true);
        }

        public static void Update(int id, string name, NameLookupViewModel model)
        {
            DictionaryItem item = model._adapter.GetItem(id);
            item.Name = name;
            model._adapter.Save(item, false);
        }

        public static void Delete(int id, NameLookupViewModel model)
        {
            model._adapter.Delete(id);
        }
        #endregion Data Source
    }
}
